package com.example.workmode.UserPreferences;


import com.example.workmode.Alarm.RegisterAlarm;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class RefreshTimeSetting extends Activity {

	String title;
	String varHour;
	String varMinute;
	SharedPreferences pref;
	int defaultHour;
	int defaultMinute;
	int currentHour;
	int currentMinute;
	
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        
        pref = getSharedPreferences("MyPrefs", 0);
        defaultHour = pref.getInt("alarmHour",	9);
        defaultMinute = pref.getInt("alarmMinute", 0);
        title = "Set Refresh Time";
        varHour = "alarmHour";
        varMinute = "alarmMinute";
        	
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        LinearLayout layout = new LinearLayout(this);
        TimePicker timePicker = new TimePicker(this);

        timePicker.setClickable(false);
        timePicker.setEnabled(true);
        timePicker.setIs24HourView(true);
        timePicker.setCurrentHour(defaultHour);
        timePicker.setCurrentMinute(defaultMinute);
        timePicker.setScrollContainer(true);
        timePicker.setForegroundGravity(Gravity.CENTER);
        timePicker.setOnTimeChangedListener(new OnTimeChangedListener() {
   
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				currentHour = hourOfDay;
                currentMinute = minute;
			}
   
        });
        layout.setGravity(Gravity.CENTER);
        layout.addView(timePicker);
        alert.setTitle(title);
        alert.setView(layout);
              
        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
        
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				SharedPreferences pref = getSharedPreferences("MyPrefs", 0);
				SharedPreferences.Editor editor = pref.edit(); 
				editor.putInt(varHour, currentHour);
				editor.putInt(varMinute, currentMinute);
				editor.commit();
				Intent i = new Intent(getBaseContext(), RegisterAlarm.class);
				i.putExtra("msg", "regAgain");
				sendBroadcast(i);
			}
		});
        
        alert.show();
   }
}
